﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photon.SocketServer;
using WorldOfSuperpowers.Cache;
using WorldOfSuperpowers_COMMON;
using WorldOfSuperpowers_COMMON.DataModel;

namespace WorldOfSuperpowers.Handler
{
    public class MatchHandler : BaseHandler
    {
        public override void InitCode()
        {
            code = OperationCode.Match;
        }

        public override void TranslateDic(Dictionary<byte, object> message, MasterClientPeer peer,
            SendParameters sendParameters)
        {
            object Vcontent = new object();
            bool hasGet = message.TryGetValue((byte) code, out Vcontent);
            //判断是匹配中的哪一个消息、
            if (hasGet)
            {
                OperationCode Mcode = (OperationCode) Vcontent;
                object data = GetValueFromDic(Mcode, message);
                switch (Mcode)
                {
                    //玩家开始/取消匹配操作
                    case OperationCode.ReadyInMatchQueue:
                        if (data != null)
                        {
                            OperationResponse op_R = new OperationResponse();
                            op_R.OperationCode = (byte) code;
                            op_R.DebugMessage = "进入匹配队列";
                            op_R.ReturnCode = (byte) ReturnCode.Success;
                            Dictionary<byte, object> parameter_R = new Dictionary<byte, object>();
                            parameter_R.Add((byte) OperationCode.Match, Mcode);
                            op_R.Parameters = parameter_R;
                            peer.SendOperationResponse(op_R, sendParameters);
                            //添加进匹配列表
                            MatchCenter.GetInstance().PutNewPlayerInQueueForTwo(data.ToString());
                        }
                        break;
                    case OperationCode.DivideMatch:
                        if (data != null)
                        {
                            OperationResponse op_D = new OperationResponse();
                            op_D.OperationCode = (byte) code;
                            op_D.DebugMessage = "离开匹配队列";
                            op_D.ReturnCode = (byte) ReturnCode.Success;
                            Dictionary<byte, object> parameter_D = new Dictionary<byte, object>();
                            parameter_D.Add((byte) OperationCode.Match, Mcode);
                            op_D.Parameters = parameter_D;
                            peer.SendOperationResponse(op_D, sendParameters);
                            //从匹配队列移除
                            MatchCenter.GetInstance().RemovePlayerFromQueue(data.ToString());
                        }
                        break;
                    case OperationCode.ReadyForMatch:
                        if (data != null)
                        {
                            ReadyForMatch Rmodel = ProtoTranslater.GetInstance().GetTypeFromBytes<ReadyForMatch>(data);
                            MatchCenter.GetInstance().GetRoomReadyMessage(Rmodel.MatchRoomIndex,Rmodel.MyMatchPos);
                        }
                        break;
                    case OperationCode.SkillID:
                        if (data != null)
                        {
                            SkillModel Smodel = ProtoTranslater.GetInstance().GetTypeFromBytes<SkillModel>(data);
                            MatchCenter.GetInstance().GetRoomBPMessage(Smodel);
                        }
                        break;
                    case OperationCode.BeginGame:
                        MasterServer.log.Debug("倒计时结束，收到双方游戏开始反馈");
                        break;
                }
            }
        }
    }
}
